var HuoChaiTypes = require('HuoChaiTypes');
var gamelevels = HuoChaiTypes.gamelevels;
var gamelevel = 0;//真实显示关卡数
var havegamelevel = 163;//有的管卡数关卡数
var falsegamelevel = gamelevel % havegamelevel;//根据有限关卡设置成无限循环
//var MainGame =
cc.Class({
    extends: cc.Component,

    properties: {
        audioMng: cc.Node,
        TxtGuanshu: cc.RichText,
        TxtTiShi: cc.RichText,
        btnDashangTu: cc.Node,
        shuzi0: cc.Node,
        shuzi1: cc.Node,
        shuzi2: cc.Node,
        shuzi3: cc.Node,
        BarLv: {
            type: cc.ProgressBar,
            default: null
        },
        jumper: {
            default: null,
            type: cc.Node
        },
        isPause: false,
    },

    // use this for initialization
    onLoad: function () {
        this.audioMng = this.audioMng.getComponent('AudioMng');
        this.listenerStorageSyncGet();
        this.restartGame();
        cc.log('Next scene preloaded');
    },
    //清空界面 准备开始，或者重新开始
    restartGame: function () {
        this.isPause = false;
        this.BarLv.progress = 0.0;
        falsegamelevel = gamelevel % havegamelevel;
        this.TxtGuanshu.string = "<color=#ffffff>第</c><color=#0fffff>" + (gamelevel + 1) + "</color><color=#ffffff>关</c>";
        this.TxtTiShi.string = "";
        this.btnDashangTu.active = false;
        this.initGame(gamelevels[falsegamelevel]);
    },

    //初始化数字
    initGame: function (levels) {

        this.shuzi0 = this.shuzi0.getComponent('ShuZhi');
        this.shuzi0.init(levels[0]);
        this.shuzi1 = this.shuzi1.getComponent('ShuZhi');
        this.shuzi1.init(levels[1]);
        this.shuzi2 = this.shuzi2.getComponent('ShuZhi');
        this.shuzi2.init(parseInt(levels[2] / 10));
        this.shuzi3 = this.shuzi3.getComponent('ShuZhi');
        this.shuzi3.init(levels[2] % 10);
    },

    update: function () {
        cc.log('Next scene preloaded');
        //console.log('this.shuzi0.getNum()============' + this.shuzi0.getNum());
        //符合正确答案a+b=c
        //console.log("falsegamelevel================" + falsegamelevel);
        if (this.shuzi0.getNum() == gamelevels[falsegamelevel][3]
            && this.shuzi1.getNum() == gamelevels[falsegamelevel][4]
            && (this.shuzi2.getNum() * 10 + this.shuzi3.getNum()) == gamelevels[falsegamelevel][5]) {
                console.log("----->1");
            this.HandleWinEvent();
        }
        //符合正确答案加法交换律 b+a=c
        else if (this.shuzi0.getNum() == gamelevels[falsegamelevel][4]
            && this.shuzi1.getNum() == gamelevels[falsegamelevel][3]
            && (this.shuzi2.getNum() * 10 + this.shuzi3.getNum()) == gamelevels[falsegamelevel][5]) {
                console.log("----->2");
            this.HandleWinEvent();
        }
        //正确答案也猜不到的答案 等式只要成立就好
        else if (this.shuzi0.getNum() + this.shuzi1.getNum() == (this.shuzi2.getNum() * 10 + this.shuzi3.getNum())) {
            console.log("----->3");
            this.HandleWinEvent();
        }
        //三个数字 有任何一个组不成数字 失败 重新开始
        else if (this.shuzi0.getNum() == -1 || this.shuzi1.getNum() == -1 || this.shuzi2.getNum() == -1 || this.shuzi3.getNum() == -1) {
            this.initGame(gamelevels[falsegamelevel]);
        }
        //三个数字能组成数字，但是等式不成立 失败 重新开始
        else if (this.shuzi0.getNum() != gamelevels[falsegamelevel][0]
            || this.shuzi1.getNum() != gamelevels[falsegamelevel][1]
            || (this.shuzi2.getNum() * 10 + this.shuzi3.getNum()) != gamelevels[falsegamelevel][2]) {
            this.initGame(gamelevels[falsegamelevel]);
        }

    },

    HandleWinEvent:function(){
        if (this.isPause == false)
        {
            this.isPause = true;
            this.audioMng.playWin();
            var seqBarAdd = cc.sequence(cc.callFunc(this.callProgressAdd.bind(this)),cc.delayTime(0.1));
            var seqBar = cc.repeat(seqBarAdd,10);
            var seq = cc.sequence(seqBar,cc.callFunc(this.callFunctionEnd.bind(this)));
            this.jumper.stopAction();
            this.jumper.runAction(seq);
        }

    },

    callProgressAdd:function(){
        if (this.BarLv.progress <= 1)
        {
            this.BarLv.progress += 0.1;
        }
        else
        {
            this.BarLv.progress = 1;
        }

    },

    callFunctionEnd:function(){
        gamelevel++;
        this.listenerStorageSyncSave();
        this.restartGame();
    },

    clickbtnBack: function () {
        cc.director.loadScene('mainmenmu');
    },
    clickbtnTishi: function () {
        //this.TxtTiShi.string = "<color=#ffffff>" + gamelevels[falsegamelevel][3] + "</c><color=#0fffff>+</color><color=#ffffff>" + gamelevels[falsegamelevel][4] + "</c><color=#0fffff>=</color><color=#ffffff>" + gamelevels[falsegamelevel][5] + "</c>";
    },
    clickbtnDashang: function () {
        this.btnDashangTu.active = !this.btnDashangTu.active;

    },
    clickbtnQiuzhu: function () {
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            wx.shareAppMessage({
                title: '我要主动拉起分享啦,会不会来玩玩啊，好玩得很！',
                imageUrl: canvas.toTempFilePathSync({
                    destWidth: 500,
                    destHeight: 400
                }),
                success: function (shareTickets, groupMsgInfos) { console.log('主动成功') },
                fail: function (res) { console.log('主动失败'); console.log(res); },
            }
            );
        }
    },

    /**
   * 数据同步存储
   */
    listenerStorageSyncSave: function () {
        console.log('数据同步存储');
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            wx.setStorageSync('gamelevel', gamelevel);
            this.setUserCloudStorage();
            //this.getFriendCloudStorage();
        }
    },

    /**
     * 数据同步获取
     */
    listenerStorageSyncGet: function () {
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            // var that = this;
            console.log('数据同步获取');
            var value = wx.getStorageSync(HuoChaiTypes.gamelevel, 0);
            gamelevel = value;

            if (gamelevel == null) {
                gamelevel = 0;
            }
            console.log('数据同步获取gamelevel=====' + gamelevel);
        }
    },

    /**
   * 数据同步存储
   */
    setUserCloudStorage: function () {
        console.log('(gamelevel+1).toString()=====' + (gamelevel+1).toString());
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            wx.setUserCloudStorage({
               
                KVDataList: [{ key: '1', value: (gamelevel+1).toString() }, { key: '2', value: '3' }],
                success: function (res) {
                    console.log('setUserCloudStorage', 'success', res)
                },
                fail: function (res) {
                    console.log('setUserCloudStorage', 'fail')
                },
                complete: function (res) {
                    console.log('setUserCloudStorage', 'ok')
                }
            });
        }
    },

    /**
     * 数据同步获取
     */
    getFriendCloudStorage: function () {
        wx.getFriendCloudStorage({
            keyList: ["1"],
            success: res => {
                //let data = res.data
                console.log("getFriendCloudStorage:", res);
            },
            fail: res => {
                console.log("getFriendCloudStorage fail:", res);
            }
        });

    },


});
